import sys
import logging
import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus_tcp as modbus_tcp
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer

logger = modbus_tk.utils.create_logger("console")
if __name__ == "__main__":
    try:
        # 连接MODBUS TCP从机
        master = modbus_tcp.TcpMaster("192.168.1.5",502)
        master.set_timeout(5.0)
        logger.info("connected")
        # 读保持寄存器
        #i=logger.info(master.execute(1, cst.READ_HOLDING_REGISTERS, 0,2))
        j = master.execute(1, cst.READ_HOLDING_REGISTERS, 0,2)
        print("j:",j)
    except ValueError:
        print("Error!")

class Ui_MainWindow(object):  # 继承父类QObject
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)  # 创建一个中央元件centralwidget，并把它加入到窗口MainWindow
        self.centralwidget.setObjectName("centralwidget")  # 命名
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)  # 命令按钮
        self.pushButton.setGeometry(QtCore.QRect(200, 60, 361, 71))
        self.pushButton.setObjectName("pushButton")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(190, 200, 401, 261))
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.timer = QTimer(self)  # 初始化一个定时器
        self.timer.timeout.connect(self.operate)  # 计时结束调用operate()方法
        self.pushButton.clicked.connect(self.buttonOnClick)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)  # 关联信号槽

    def buttonOnClick(self):
        self.timer.start(1000)  # 设置计时间隔并启动

    def operate(self):
        j = master.execute(1, cst.READ_HOLDING_REGISTERS, 0, 2)
        print("j:", j)
        self.textEdit.setText("Timer start !!!\n")

        if j[1]==0:
            self.textEdit.append("j[1] = 0 \n")
            self.case0()
        elif j[1]==1:
            self.textEdit.append("j[1] = 1 \n")
            self.case1()
        elif j[1]==2:
            self.textEdit.append("j[1] = 2 \n")
            self.case2()
        else:
            self.textEdit.append("j[1] = 3 \n")
            self.case3()


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "按钮"))

    # 定义槽函数
    def case0(self):
        self.textEdit.setText("1,2号窗户均关闭")

    def case1(self):
        self.textEdit.setText("1号窗户打开,2号窗户关闭")

    def case2(self):
        self.textEdit.setText("1号窗户关闭,2号窗户打开")

    def case3(self):
        self.textEdit.setText("1,2号窗户均被打开")

class mywindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(mywindow, self).__init__()
        self.setupUi(self)


app = QtWidgets.QApplication(sys.argv)
# MainWindow = QMainWindow()
window = mywindow()
window.show()
sys.exit(app.exec_())